package com.example.demo.service;

import com.example.demo.dao.UserRepository;
import com.example.demo.entity.User;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public Boolean addUser(User user) {
        Boolean flag = true;
        try {
            userRepository.save(user);
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
        }
        return flag;
    }

    public List<User> selectAll() {
        List<User> user = userRepository.findAll();
        return user;
    }

    public List<User> selectByUsername(String userName) {
        List<User> user = userRepository.findByUserName(userName);
        return user;
    }

    public User selectUserById(Integer id) {
        User user = userRepository.findUserById(id);
        return user;
    }

    public Boolean deleteUser(Integer id) {
        Boolean flag = true;
        try {
            userRepository.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
        }
        return flag;
    }
}